package com.cayte.location;

import java.util.List;

import android.app.ActivityManager;
import android.app.Service;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

public class CayteLocationService extends Service implements Runnable {
	private static final String TAG = "CayteLocationService";

	private static boolean serviceState = false;
	private static CayteLocationHelper lbs;
	private static CayteLocation locObj = null;
	private ActivityManager activityManager;
	private String runPkgName = null;

	private final CayteLocationAIDLServic.Stub binder = new CayteLocationAIDLServic.Stub() {

		@Override
		public String getLocJson() throws RemoteException {
			// TODO Auto-generated method stub
			if (locObj != null)
				return locObj.toJson();
			return null;
		}

		@Override
		public boolean isSericeRun() throws RemoteException {
			// TODO Auto-generated method stub
			return serviceState;
		}

		@Override
		public void setMinTime(long minTime) throws RemoteException {
			// TODO Auto-generated method stub
			lbs.setMinTime(minTime);
		}

	};

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		runPkgName = intent.getStringExtra("packageName");
		return binder;
	}

	@Override
	public boolean onUnbind(Intent intent) {
		// TODO Auto-generated method stub
		return super.onUnbind(intent);
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		Log.e(TAG, "onCreate");
		serviceState = true;
		activityManager = (ActivityManager) this
				.getSystemService(Context.ACTIVITY_SERVICE);

		lbs = new CayteLocationHelper(this);

		lbs.requestAllLocationUpdates();

		Thread thread = new Thread(this);
		thread.start();
	}

	@Override
	public void onDestroy() {
		lbs.removeAllUpdates();
		serviceState = false;
		Log.e(TAG, "onDestroy");
		super.onDestroy();
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		CayteLocation loc = null;
		Log.e(TAG, "Runnable run");
		while (serviceState) {
			loc = null;
			loc = lbs.getNetworkLocation(locObj);
			for (String p : lbs.providers) {
				CayteLocation l = null;
				if (p.equals(LocationManager.NETWORK_PROVIDER)) {
					l = lbs.getLocation();
				} else if (p.equals(LocationManager.GPS_PROVIDER)) {
					l = lbs.getLocation();
				}
				loc = thanLoc(loc, l);
			}
			setLocObj(loc);

			lbs.sleep();

			if (!isAppRun(runPkgName)) {
				Log.e(TAG, "app not run");
				onDestroy();
				break;
			}
		}
	}

	private boolean isAppRun(String packageName) {
		if (packageName == null)
			return false;
		List<RunningAppProcessInfo> appProcesses = activityManager
				.getRunningAppProcesses();
		if (appProcesses == null)
			return false;
		for (RunningAppProcessInfo appProcess : appProcesses) {
			if (appProcess.processName.equals(packageName)) {
				return true;
			}
		}
		return false;
	}

	private CayteLocation thanLoc(CayteLocation... locs) {
		// TODO Auto-generated method stub
		CayteLocation loc = null;
		for (CayteLocation l : locs) {
			if (l != null) {
				if (loc == null) {
					loc = new CayteLocation(l);
				} else {
					if (loc.accuracy > l.accuracy) {
						loc = new CayteLocation(l);
					}
				}
			}
		}
		return loc;
	}

	private void setLocObj(CayteLocation loc) {
		if (loc == null)
			return;
		locObj = new CayteLocation(loc);
	}

}
